package com.rchat.pocmini.activity.recharge;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.rchat.pocmini.R;
import com.rchat.pocmini.bean.RechargeResultBean;
import com.rchat.pocmini.common.mvp.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Description ：充值中心
 * @Author itsdf07
 * @Time 2017/9/27
 */

public class ReChargeMvpActivity extends BaseMvpActivity<ReChargePresenterImpl, ReChargeModelImpl> implements
        RechargeContract.View {


    @BindView(R.id.et_recharge_charge_month_num)
    EditText etRechargeMonthNum;

    @BindView(R.id.btn_recharge_month)
    Button btnRechargeMonth;

    @BindView(R.id.btn_recharge_one_year)
    Button btnRechargeOneYear;

    @BindView(R.id.btn_recharge_two_year)
    Button btnRechargeTwoYear;

    @BindView(R.id.btn_recharge_three_year)
    Button btnRechargeThreeYear;

    @BindView(R.id.lv_recharge_result)
    ListView lvRechargeResult;

    private RechargeResultAdapter adapter;

    private List<RechargeResultBean.DataBean.OrdersBean> data;



    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    public void initView() {
        btnRechargeMonth.setClickable(false);
        btnRechargeOneYear.setClickable(false);
        btnRechargeTwoYear.setClickable(false);
        btnRechargeThreeYear.setClickable(false);
    }

    @Override
    public void doAfterInitView() {
        data = new ArrayList<>();
        adapter = new RechargeResultAdapter(data,this);
        lvRechargeResult.setAdapter(adapter);
        mPresenter.queryPriceList();
        mPresenter.queryRechargeResult();
    }

    @OnClick({R.id.iv_back,
              R.id.btn_recharge_month,
              R.id.btn_recharge_one_year,
              R.id.btn_recharge_two_year,
              R.id.btn_recharge_three_year,
              R.id.tv_refresh})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                mPresenter.updateAccountDate();
                doGoBack();
                break;
            case R.id.btn_recharge_month:
                mPresenter.recharge(ReChargePresenterImpl.RECHARGE_MONTH,etRechargeMonthNum.getText().toString());
                break;
            case R.id.btn_recharge_one_year:
                mPresenter.recharge(ReChargePresenterImpl.RECHARGE_ONE_YEAR,"");
                break;
            case R.id.btn_recharge_two_year:
                mPresenter.recharge(ReChargePresenterImpl.RECHARGE_TWO_YEAR,"");
                break;
            case R.id.btn_recharge_three_year:
                mPresenter.recharge(ReChargePresenterImpl.RECHARGE_THREE_YEAR,"");
                break;
            case R.id.tv_refresh:
                mPresenter.queryRechargeResult();
                break;
        }
    }

    @Override
    public void showPrice(String month, String oneYear, String twoYear, String threeYear) {
//        btnRechargeMonth.setText(month);
        btnRechargeOneYear.setText(oneYear);
        btnRechargeTwoYear.setText(twoYear);
        btnRechargeThreeYear.setText(threeYear);
        btnRechargeMonth.setClickable(true);
        btnRechargeOneYear.setClickable(true);
        btnRechargeTwoYear.setClickable(true);
        btnRechargeThreeYear.setClickable(true);

    }

    @Override
    public void showRechargeInfoDialog(String moneyForOneMonth,String moneyForOneYear,String moneyForTwoYear,String moneyForThreeYear) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
        normalDialog.setMessage("充值规则提醒如下：1.按月充值，您购买的月数必须是小于12的整数，价格为1个月" + moneyForOneMonth + "元；" +
                "2.按年充值，直接选择购买1年，2年或者3年，价格分别为" + moneyForOneYear + "元，" + moneyForTwoYear + "元，" + moneyForThreeYear + "元，此种购买方式享有一定的优惠。");
        normalDialog.setPositiveButton(R.string.no_tip_again,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.noShowRechargeInfoNextTime(true);
                    }
                });
        normalDialog.setNegativeButton(R.string.register_back,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do

                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    public void showRechargeResult(List<RechargeResultBean.DataBean.OrdersBean> orders) {
        data.clear();
        for(RechargeResultBean.DataBean.OrdersBean b : orders){
            if(b.getStatus() == 0){
                continue;
            }
            data.add(b);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mPresenter.updateAccountDate();
    }
}
